<?php


    $link = mysqli_connect("localhost","root","") or die("数据连接失败");
    mysqli_set_charset($link,"utf8");
    mysqli_select_db($link,"bbs");
    switch($_GET['a']){
        case 'del':
            $id = $_GET['id'];
            $sql = "delete from user where id={$id}";
            $result = mysqli_query($link,$sql);

            if($result && mysqli_affected_rows($link)>0){
                echo "<script>alert('删除成功！');window.location.href='./main_list.php';</script>";
                die;
            }else{
                echo "<script>alert('删除失败！');window.location.href='./main_list.php';</script>";
                die;
            }

            break;
        case 'add':
            $username = $_POST['username'];
            $password = $_POST['password'];
            $auth = $_POST['level'];
            $upassword = $_POST['upassword'];
            if(!empty($username) && !empty($password)) {
                if($password==$upassword){
                    $sql = "insert into user(userName,password,auth,lastlogin) values('$username','$password','$auth',20161018)";
                    $result = mysqli_query($link,$sql);
                    if($result && mysqli_affected_rows($link)>0){
                        echo "<script>alert('添加成功！');window.location.href='./main_list.php';</script>";
                        die;
                    }else{
                        echo "<script>alert('添加失败！');window.location.href='./main_info.php';</script>";
                        die;
                    }
                }else{
                    echo "<script>alert('确认密码与密码不一致！');window.location.href='./main_info.php';</script>";
                    die;
                }


            }else{
                echo "<script>alert('账号和密码都不能为空');window.location.href='./main_info.php';</script>";
            }
            break;
        case 'edit':
            $id = $_GET['id'];
            $auth = $_POST['level'];
            $status = $_POST['status'];
//            var_dump($id);
//            var_dump($auth);
//            var_dump($status);
//            die();
            $sql = "update user set auth='{$auth}',status='{$status}' where id={$id}";
            mysqli_query($link,$sql);
            if(mysqli_affected_rows($link)>0){
                echo "<script>alert('修改成功！');window.location.href='./main_list.php';</script>";
                die;
            }else{
                echo "<script>alert('修改失败！');window.location.href='./main_list.php';</script>";
                die;
            }
            break;
    }
    mysqli_close($link);